<template>
    <div class="box">
        <h1 style="text-align: center;">角色列表</h1>
        <router-link to="/role/add">
            <el-button type="primary" v-permission="{ path: '/role/add', name: '角色添加' }">添加</el-button>
        </router-link>
        <el-table :data="rolelist" height="250" border style="width: 100%">
            <el-table-column prop="name" label="角色名称"></el-table-column>
            <el-table-column prop="auths" label="角色权限">
                <template slot-scope="scope">
                    <el-tree :data="scope.row.auths" :props="defaultProps"></el-tree>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
                        v-permission="{ path: '/role/edit', name: '角色修改' }">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
                        v-permission="{ path: '/role/del', name: '角色删除' }">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { roleList, roleDel } from '@/request'
import permission from '@/utils/permission'

export default {
    directives: {
        permission
    },
    data() {
        return {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            rolelist: []
        }
    },
    methods: {
        handleEdit(index, row) {
            this.$router.push('/role/edit/' + row._id)
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                roleDel(row._id).then(data => {
                    let { error_code, msg } = data
                    if (error_code === 200) {
                        this.rolelist.splice(index, 1)
                        this.$message({
                            type: 'success',
                            message: msg
                        });
                    } else {
                        this.$message({
                            type: 'error',
                            message: msg
                        });
                    }
                })
            })
        },
        async getRoleList() {
            let data = await roleList()
            if (data.error_code === 200) {
                this.rolelist = data.data.rolelist
            }
        },
    },
    created() {
        this.getRoleList()
    }
}
</script>

<style>
.el-table,
.box {
    height: 90% !important
}
</style>
